package com.waiting.leetcode;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/12
 **/
public class SecondMinimumNodeInABinaryTree671 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(2);
        TreeNode a = new TreeNode(2);
        TreeNode b = new TreeNode(5);
        TreeNode c = new TreeNode(5);
        TreeNode d = new TreeNode(7);
        root.left = a;
        root.right = b;
        System.out.println(findSecondMinimumValue(root));
    }

    public static int findSecondMinimumValue(TreeNode root) {
        if (root == null) {
            return -1;
        }
        if (root.left != null) {
            int leftMin = 0, rightMin = 0;
            if (root.left.val == root.val) {
                leftMin = findSecondMinimumValue(root.left);
            } else {
                leftMin = root.left.val;
            }
            if (root.right.val == root.val) {
                rightMin = findSecondMinimumValue(root.right);
            } else {
                rightMin = root.right.val;
            }
            int min = 0;
            if (leftMin < min) {
                min = rightMin;
            } else if (rightMin < min) {
                min = leftMin;
            } else
                min = Math.min(leftMin, rightMin);
            return min;
        } else {
            return -1;
        }

    }

}